const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}
const formatDay = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('-')}`
}
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}
// 加密
// 加密方法
const AesEncode = function (word) {
  const CryptoJS = require('./crypto-js.min'); //引入crypto
  const key = CryptoJS.enc.Utf8.parse('d887aed2239884eb') // 密钥
  const iv = CryptoJS.enc.Utf8.parse('b2df5feec9d499c9') // 密钥偏移量
  let srcs = CryptoJS.enc.Utf8.parse(word);
  let encrypted = CryptoJS.AES.encrypt(srcs, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  });
  let encryptedBase64Data = CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
  return encodeURIComponent(encryptedBase64Data);
}
// 解密方法
const AesDecode = function (word) {
  const CryptoJS = require('./crypto-js.min'); //引入crypto
  const key = CryptoJS.enc.Utf8.parse('d887aed2239884eb') // 密钥
  const iv = CryptoJS.enc.Utf8.parse('b2df5feec9d499c9') // 密钥偏移量
  word = decodeURIComponent(word)
  let encryptedHexStr = CryptoJS.enc.Base64.parse(word);
  let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
  let decrypt = CryptoJS.AES.decrypt(srcs, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  });
  let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
  return decryptedStr.toString();
}
const formatDate = d => {
  var date = new Date(d);
  var YY = date.getFullYear() + '-';
  var MM =
    (date.getMonth() + 1 < 10 ?
      '0' + (date.getMonth() + 1) :
      date.getMonth() + 1) + '-';
  var DD = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  var hh =
    (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  var mm =
    (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) +
    ':';
  var ss =
    date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  return YY + MM + DD;
}
module.exports = {
  formatTime,
  formatDay,
  AesEncode,
  AesDecode,
  formatDate
}